﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PokeIn;
using PokeIn.Comet;

/// <summary>
/// Summary description for MyClient
/// </summary>
public class MyClients : IDisposable
{
    public static List<string> Users = new List<string>();
    String _clientId;

    public String ClientId
    {
        get { return _clientId; }
        set { _clientId = value; }
    }
    int notifyCount;

    public int NotifyCount
    {
        get { return notifyCount; }
        set { notifyCount = value; }
    }

    public MyClients()
    {
        notifyCount = -1;
    }

    public MyClients(String clientId)
    {
        _clientId = clientId;
    }

    public void Dispose()
    {
        //PokeIn calls this method when the client is disconnected.
        lock (Users)
        {
            Users.Remove(_clientId);
        }
    }

    public void SendToClient(String userName, ChatMessage message)
    {
        //Create JSON method from custom class
        if(Users.Contains(userName))
        {
            string json = JSON.Method("SenTo", message); 
            CometWorker.SendToClient(userName,json);
        }
    }
}

[Serializable]
public class ChatMessage
{
    public string Username, Message;

    //You should define an Empty Consturctor to allow PokeIn to de-serialize
    public ChatMessage()
    {
        Username = "";
        Message = "";
    }

    public ChatMessage(string userName, string message)
    {
        Username = userName;
        Message = message;
    }
}